const Koa = require('koa')
const logger = require('koa-logger')
const compose = require('koa-compose')
const app = new Koa();

//定义中间件
const loggerMiddleware = async (ctx, next) => {
    const start = Date.now();
    await next();
    const ms = Date.now() - start;
    console.log(ctx.method, ctx.host + ctx.url + ctx.querystring + `[${ms}ms]`)
};

const helloMiddleware = async (ctx, next) => {
    await next();
    ctx.response.type = "text/html"
    ctx.response.body = '<h1>Hello Koa!</h1>';
}

let allMiddleware = compose([loggerMiddleware, helloMiddleware]);
app.use(logger()).use(allMiddleware);

//监听3000端口
app.listen(3000, () => {
    console.log('server is running at http://localhost:3000')
});
